package com.demo.controller;


import com.demo.domain.User;
import com.demo.domain.UserCustom;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller   //<bean class="UserController"/>
@RequestMapping("/user")
public class UserController  {
    
    @RequestMapping(value="/hello.do",method = {RequestMethod.POST,RequestMethod.GET})
    public String myHello(){
        
        return "hello";
    }
    
    @RequestMapping("toAdd")
    public String toAdd(Integer id){
        System.out.println(id);
        return "add";
    }
    //接受int类型参数
    @RequestMapping("receiveInt")
    public String receiveInt(@RequestParam(defaultValue = "222",value = "ss",required = true) Integer id){
        System.out.println(id);
        return "success";
    }
    //接受字符类型参数
    @RequestMapping("receiveStr")
    public String receiveStr(String username){
        System.out.println(username);
        return "success";
    }
    //接受参数封装User对象
    @RequestMapping("receiveUser")
    public String receiveUser(User user){
        System.out.println(user);
        return "success";
    }
    //接受包装类型参数
    @RequestMapping("receiveUserCustom")
    public String receiveUserCustom(UserCustom user){
        System.out.println(user);
        return "success";
    }
    //接收数组参数
    @RequestMapping("receiveArray")
    public String receiveIds(Integer[] ids){
        System.out.println(ids);
        return "success";
    }
    //接收list集合参数
    @RequestMapping("receiveUserCustomList")
    public String receiveUserCustomList(UserCustom user){
        System.out.println(user);
        return "success";
    }
    //接收map集合参数
    @RequestMapping("receiveUserCustomMaps")
    public String receiveUserCustomMaps(UserCustom user){
        System.out.println(user);
        return "success";
    }
    
    //页面回显
    @RequestMapping("list")
    public String list(Model model){
        List<User> list=new ArrayList<>();
        
        User user=new User();
        user.setId(1);
        user.setUsername("张三");
        user.setAddress("南京");
        user.setBirthday(new Date());
        user.setAge(12);
    
        User user2=new User();
        user2.setId(2);
        user2.setUsername("张三2");
        user2.setAddress("南京");
        user2.setBirthday(new Date());
        user2.setAge(13);
    
        User user3=new User();
        user3.setId(3);
        user3.setUsername("张三2");
        user3.setBirthday(new Date());
        user3.setAddress("南京");
        user3.setAge(14);
        list.add(user);
        list.add(user2);
        list.add(user3);
        
        model.addAttribute("userList",list);
        return "list";
    }
    //修改
    @RequestMapping("update/{id}")
    public String update(@PathVariable Integer id){
        System.out.println(id);
        return "redirect:/user/list.do";
    }
    //测试forward
    @RequestMapping("forward")
    public String forward(){
        return "forward:/items/list.do";
    }
    //测试redirect
    @RequestMapping("redirect")
    public String redirect(){
        return "redirect:list.do";
    }
    
    //跳转到requestJson界面
    @RequestMapping("toJson")
    public String toJson(){
        return "requestJson";
    }
    //接收json字符串数据，使用RequestBody将json格式数据封装到user对象
    //返回user对象，使用ResponseBody把user对象转换成json对象
    @RequestMapping(value="/requestJson",method = RequestMethod.POST)
    public @ResponseBody User requestJson(@RequestBody User user){
        System.out.println(user);
        return user;
    }
    //请求pojo，返回json
    @RequestMapping(value="/requestPojo",method = RequestMethod.POST)
    public @ResponseBody User requestPojo(User user){
        System.out.println(user);
        return user;
    }
    
    //对多视图支持
    @RequestMapping("multiView")
    public User multiView(){
        User user=new User();
        user.setId(1);
        user.setUsername("张三");
        user.setAddress("南京");
        user.setBirthday(new Date());
        user.setAge(12);
    
        return user;
    }
}

